#pragma once

#include "IEventHandler.h"
#include "IInteraction.h"
#include "workspace/IView.h"
#include "messenging/event/args/SystemEventArgs.h"
#include "EventInfo.h"

interface Graphics; interface IInteraction;

class EventHandler : public IEventHandler
{
public:
	EventHandler()
	{
		StartInteractionInput(NULL,NULL);
		eventInfo = NULL;
	}
	virtual ~EventHandler(){}

	BEGIN_DEFINE_CLASS(CLASSID_NULL)
		DEFINE_INTERFACE_ENTRY(IEventHandler)
	END_DEFINE_CLASS()

	void StartInteractionInput(Graphics* graphics,IInteraction* interaction)
	{
		this->graphics  = graphics;
		this->interaction = interaction;
	}

	virtual void EventLoopBegin(){}
	virtual void Draw(){}
	virtual void EventLoopEnd()
	{
		delete eventInfo;
	}
	virtual void SetEventInfo(EventInfo* info)
	{
		ASSERT(info);
		eventInfo = info->Clone();
	}

protected:
	virtual bool OnLButtonDown(SystemEventArgs* e){e;return false;}
	virtual bool OnLButtonUp(SystemEventArgs* e){e;return false;}
	virtual bool OnLButtonDoubleClick(SystemEventArgs* e){e;return false;}
	virtual bool OnRButtonDown(SystemEventArgs* e){e;return false;}
	virtual bool OnRButtonUp(SystemEventArgs* e){e;return false;}
	virtual bool OnRButtonDoubleClick(SystemEventArgs* e){e;return false;}
	virtual bool OnMouseMove(SystemEventArgs* e){e;return false;}
	virtual bool OnMouseWheel(SystemEventArgs* e){e;return false;}

	Graphics* graphics;
	IInteraction* interaction;
	EventInfo* eventInfo;

private:
	virtual bool Event(SystemEventArgs* e)
	{
		if (e->LButtonUp())
		{
			if (OnLButtonUp(e))
				return true;
		}
		else if (e->LButtonDn())
		{
			if (OnLButtonDown(e))
				return true;
		}
		else if (e->LButtonDblClick())
		{
			if (OnLButtonDoubleClick(e))
				return true;
		}
		if (e->RButtonUp())
		{
			if (OnRButtonUp(e))
				return true;
		}
		else if (e->RButtonDn())
		{
			if (OnRButtonDown(e))
				return true;
		}
		else if (e->RButtonDblClick())
		{
			if (OnRButtonDoubleClick(e))
				return true;
		}
		else if (e->MouseMove())
		{
			if (OnMouseMove(e))
				return true;
		}
		else if (e->MouseWheel())
		{
			if (OnMouseWheel(e))
				return true;
		}
		return false;
	}
};